package com.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.management.commons.exception.ExceptionCode.ProjectExceptionCode;
import com.management.commons.exception.ProjectException;
import com.management.controller.form.*;
import com.management.controller.info.GetMyProjectDescInfo;
import com.management.controller.info.GetProjectByNameInfo;
import com.management.controller.info.ProjectPageInfo;
import com.management.model.Account;
import com.management.model.DefectRecord;
import com.management.model.ProjectInfo;
import com.management.dao.ProjectInfoDao;
import com.management.service.AccountService;
import com.management.service.DefectRecordService;
import com.management.service.ProjectInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.management.utils.CopyUtils;
import com.management.utils.Result;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 龙世超
 * @since 2022-03-23
 */
@Service
public class ProjectInfoServiceImpl extends ServiceImpl<ProjectInfoDao, ProjectInfo> implements ProjectInfoService {

    @Resource
    @Lazy
    private AccountService accountService;

    @Resource
    private ProjectInfoDao projectInfoDao;

    @Resource
    private DefectRecordService defectRecordService;
    /**
     * 获取当前所有项目
     * @return
     * @param projectPageForm
     */
    @Override
    public Result getAllProjects(ProjectPageForm projectPageForm) {
        ProjectPageForm page = page(projectPageForm);
        List<ProjectPageInfo> list = CopyUtils.CopyList(page.getRecords(), ProjectPageInfo.class);
        Page<ProjectPageInfo> result=new Page<>();
        result.setRecords(list);
        result.setCurrent(page.getCurrent());
        result.setPages(page.getPages());
        result.setTotal(page.getTotal());
        result.setSize(page.getSize());
        return Result.getInstance(Page.class).setData(result);
    }

    @Override
    public Result addProject(AddProjectForm addProjectForm) {
        if (addProjectForm.getProjectName().equals("")||addProjectForm.getProjectName()==null
        ||addProjectForm.getProjectDesc().equals("")||addProjectForm.getProjectDesc()==null){
            throw new ProjectException(ProjectExceptionCode.ADDPROJECT_ERROR2);
        }
        QueryWrapper<ProjectInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_name",addProjectForm.getProjectName());
        ProjectInfo one = getOne(queryWrapper);
        if(one!=null){
        throw new ProjectException(ProjectExceptionCode.PROJECTNAME_EXISTS_ERROR);
        }
        Account byId = accountService.getById(addProjectForm.getAccountId());
        ProjectInfo projectInfo = new ProjectInfo();
        projectInfo.setCreateBy(byId.getAccountName());
        projectInfo.setModifyBy(byId.getAccountName());
        projectInfo.setProjectDesc(addProjectForm.getProjectDesc());
        projectInfo.setProjectName(addProjectForm.getProjectName());
        boolean save = save(projectInfo);
        if(save){
            return Result.getInstance().setCode(200).setMessage("添加成功");
        }
        throw  new ProjectException(ProjectExceptionCode.ADDPROJECT_ERROR);
    }




    @Override
    public Result findAllProject() {
        List<ProjectInfo> list = list();
        return Result.getInstanceList(ProjectInfo.class).setData(list);
    }

    @Override
    public Result closeProject(Integer projectId) {
        ProjectInfo byId = getById(projectId);
        if(byId.getProjectState().equals("有效")){
            byId.setProjectState("禁用");
        }else {
            byId.setProjectState("有效");
        }
        boolean b = saveOrUpdate(byId);
        if(b){
            return Result.getInstance().setCode(200).setMessage("操作成功");
        }
        throw new ProjectException(ProjectExceptionCode.CHANGEPROJECTSTATE_ERROR);
    }

    @Override
    public Result findProjectByName(GetProjectByNameForm getProjectByNameForm) {
        QueryWrapper<ProjectInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("project_name",getProjectByNameForm.getProjectName());
        GetProjectByNameForm page = page(getProjectByNameForm, queryWrapper);
        List<GetProjectByNameInfo> list = CopyUtils.CopyList(page.getRecords(), GetProjectByNameInfo.class);
        Page<GetProjectByNameInfo> project=new Page<>();
        project.setRecords(list);
        project.setPages(page.getPages());
        project.setCurrent(page.getCurrent());
        project.setTotal(page.getTotal());
        project.setSize(page.getSize());
        return Result.getInstance(Page.class).setData(project);
    }

    @Override
    public Result changeProject(ChangeProjectForm changeProjectForm) {
        if(changeProjectForm.getProjectDesc()==null||changeProjectForm.getProjectDesc().equals("")||changeProjectForm.getProjectName()==null
        ||changeProjectForm.getProjectName().equals("")){
            return Result.getInstance().setCode(700).setMessage("请输入内容");
        }
        ProjectInfo projectInfo = CopyUtils.CopyOne(changeProjectForm, ProjectInfo.class);
        boolean b = saveOrUpdate(projectInfo);
        if(b){
            return Result.getInstance().setCode(200).setMessage("修改成功");
        }
        throw new ProjectException(ProjectExceptionCode.CHANGEPROJECT_ERROR);
    }

    @Override
    public Result checkProjectState(Integer projectId) {
        ProjectInfo byId = getById(projectId);
        if(byId.getProjectState().equals("关闭")){
            throw new ProjectException(ProjectExceptionCode.PROJECTSTATE_ERROR);
        }
        return Result.getInstance().setCode(200);
    }

    @Override
    public Result getMyProjectDesc(GetMyProjectDescForm getMyProjectDescForm) {
        QueryWrapper<DefectRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("assignee",getMyProjectDescForm.getAccountName());
        queryWrapper.eq("project_name",getMyProjectDescForm.getProjectName());
        GetMyProjectDescForm page = defectRecordService.page(getMyProjectDescForm, queryWrapper);
        List<DefectRecord> records = page.getRecords();
        List<GetMyProjectDescInfo> list = CopyUtils.CopyList(page.getRecords(), GetMyProjectDescInfo.class);
        Page<GetMyProjectDescInfo> infoPage=new Page<>();
        infoPage.setPages(page.getPages());
        infoPage.setCurrent(page.getCurrent());
        infoPage.setSize(page.getSize());
        infoPage.setTotal(page.getTotal());
        infoPage.setRecords(list);
        return Result.getInstance(Page.class).setData(infoPage);
    }
}
